<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-shop"></i>
          养生内容列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-form
        class="form"
        :model="formData"
        ref="formRef"
        inline
        label-suffix=":"
      >
        <el-form-item label="标题">
          <el-input
            size="small"
            v-model="formData.titleKeyword"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select
            v-model="formData.auditStatus"
            placeholder="请选择审核状态"
            size="small"
          >
            <el-option
              v-for="item in auditStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleSearch"
            >搜索
          </el-button>
          <el-button size="small" icon="el-icon-refresh" @click="handleReset"
            >重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        border
        class="table"
        :max-height="tableHeight"
      >
        <el-table-column
          prop="title"
          label="标题"
          min-width="150"
        ></el-table-column>
        <el-table-column
          prop="summary"
          label="摘要"
          min-width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="封面" width="120">
          <template #default="scope">
            <el-image
              v-if="scope.row.coverUrl"
              :src="scope.row.coverUrl"
              :preview-src-list="[scope.row.coverUrl]"
              style="width: 80px; height: 60px"
              fit="cover"
            ></el-image>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="作者信息" width="180">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-avatar
                v-if="scope.row.authorAvatar"
                :src="scope.row.authorAvatar"
                :size="40"
                fit="cover"
                style="margin-right: 10px"
              ></el-avatar>
              <span>{{ scope.row.author || "-" }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="sortOrder"
          label="排序"
          width="80"
        ></el-table-column>
        <el-table-column label="视频" width="80">
          <template #default="scope">
            <el-tag v-if="scope.row.videoUrl" size="small" type="success"
              >有</el-tag
            >
            <el-tag v-else size="small" type="info">无</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="160"
        ></el-table-column>
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleView(scope.row)"
              >详情</el-button
            >
            <el-button type="text" size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
          :current-page="formData.offset"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="formData.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getContentList } from "@/api/regimen";

export default {
  data() {
    return {
      tableData: [],
      formData: {
        offset: 1,
        limit: 10,
        titleKeyword: "",
        auditStatus: null,
      },
      total: 0,
      auditStatusOptions: [
        { label: "全部", value: null },
        { label: "待审核", value: 0 },
        { label: "审核通过", value: 1 },
        { label: "审核拒绝", value: 2 },
      ],
    };
  },
  computed: {
    tableHeight() {
      return window.innerHeight - 400;
    },
  },
  mounted() {
    this.getContentData();
  },
  methods: {
    // 获取内容列表
    getContentData() {
      getContentList({
        offset: this.formData.offset,
        limit: this.formData.limit,
        auditStatus: this.formData.auditStatus,
        titleKeyword: this.formData.titleKeyword,
      }).then((res) => {
        if (res.code === 2000) {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        }
      });
    },
    // 搜索
    handleSearch() {
      this.formData.offset = 1;
      this.getContentData();
    },
    // 重置搜索条件
    handleReset() {
      this.formData.titleKeyword = "";
      this.formData.offset = 1;
      this.formData.auditStatus = null;
      this.getContentData();
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.formData.limit = val;
      this.formData.offset = 1;
      this.getContentData();
    },
    // 页码改变
    handlePageChange(val) {
      this.formData.offset = val;
      this.getContentData();
    },
    // 查看详情
    handleView(row) {
      this.$router.push({
        path: "/regimen/contentDetail/contentDetail",
        query: {
          id: row.id,
        },
      });
    },
    // 编辑
    handleEdit(row) {
      this.$message.info(`编辑 ID: ${row.id}`);
      // TODO: 实现编辑功能
    },
    // 删除
    handleDelete(row) {
      this.$confirm(`确定要删除"${row.title}"吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // TODO: 调用删除接口
          this.$message.success("删除成功");
          this.getContentData();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
